package cn.com.egova.demoapplication.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import cn.com.egova.demoapplication.bean.Point;
import cn.com.egova.demoapplication.tools.PointEvaluator;

/**
 * Created by yuanchao on 2017/3/8.
 */

public class ShowView extends View {
    public static final String TAG = "ShowView";
    private Paint paint;
    public static final int RADIUS = 80;
    private Point point;

    public ShowView(Context context) {
        this(context,null);
    }

    public ShowView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if(point==null){
            point = new Point(RADIUS,RADIUS);                                   //起始位置
            Point point1 = new Point(0,0);                                      //开始动画的位置
            Point point2 = new Point(getWidth()-RADIUS,getHeight()-RADIUS);     //结束动画的位置(从屏幕左上角运动到右下角)
            ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(),point1,point2);
            animator.setDuration(3000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    point = (Point) animation.getAnimatedValue();
                    invalidate();
                }
            });


            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    //结束的时候我们要求重复.
                    point = null;
                    invalidate();
                }
            });
            animator.start();
        }else{
            canvas.drawCircle(point.getX(),point.getY(),RADIUS,paint);
        }
    }
}
